Imports System.Configuration
Imports System.Collections.Specialized
Imports System.Security.Cryptography
Imports System.Text
Imports nsDataProvider = KDESolutions.Providers.DataProvider

Public Class DBWebEventsProvider
    Inherits System.Web.Management.WebEventProvider

    Private _DBType, _DBServer, _DBUser, _DBPassword, _DBDatabaseName, _ConnectString As String

    Public Const cTrueValue As String = "Y"
    Public Const cFalsevalue As String = "N"

    Public Property MyDBType() As String
        Get
            Return _DBType
        End Get
        Set(ByVal value As String)
            _DBType = value
        End Set
    End Property

    Public Overrides ReadOnly Property Name() As String
        Get
            Return "KDESolutions.Providers.WebEvents.DBWebEventProvider"
        End Get
    End Property

    Public Overrides ReadOnly Property Description() As String
        Get
            Return "Provider for storing web events and errors."
        End Get
    End Property

    Public Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection)
        If (config Is Nothing) Then
            Throw New ArgumentNullException("config")
        End If
        If ((name Is Nothing) OrElse (name.Length = 0)) Then
            name = "ImageProvider"
        End If
        If String.IsNullOrEmpty(config("description")) Then
            config.Remove("description")
            config.Add("description", "KDESolutions Image provider")
        End If

        'Start of config file settings for database connection ******
        If String.IsNullOrEmpty("DataProviderGroup") Then
            Throw New ArgumentNullException("You must provide a DataProviderGroup")
        Else
            Dim DataProviderGroup As KDESolutions.Providers.DataProvider.DataProviderConfiguration = _
            CType(ConfigurationManager.GetSection(config("DataProviderGroup")), KDESolutions.Providers.DataProvider.DataProviderConfiguration)
            If String.IsNullOrEmpty(DataProviderGroup.DBType) = False Then
                _DBType = DataProviderGroup.DBType
                Me.MyDBType = DataProviderGroup.DBType
            End If
            If String.IsNullOrEmpty(DataProviderGroup.DBServer) = False Then
                _DBServer = DataProviderGroup.DBServer
            End If
            If String.IsNullOrEmpty(DataProviderGroup.DBUser) = False Then
                _DBUser = DataProviderGroup.DBUser
            End If
            If String.IsNullOrEmpty(DataProviderGroup.DBPassword) = False Then
                _DBPassword = DataProviderGroup.DBPassword
            End If
            If String.IsNullOrEmpty(DataProviderGroup.DBDatabaseName) = False Then
                _DBDatabaseName = DataProviderGroup.DBDatabaseName
            End If
            If String.IsNullOrEmpty(DataProviderGroup.ConnectString) = False Then
                _ConnectString = DataProviderGroup.ConnectString
            End If
        End If
    End Sub

    Public Overrides Sub Flush()

    End Sub

    Public Overrides Sub ProcessEvent(ByVal raisedEvent As System.Web.Management.WebBaseEvent)
        Dim DAP As New nsDataProvider.DataProvider(_DBType, _DBServer, _DBUser, _DBPassword, _DBDatabaseName, _ConnectString)
        Try
            Dim sSQL As String
            sSQL = "INSERT INTO WebEvents(EventId,EventTimeUtc,EventTime," & _
            "EventType,EventSequence,EventOccurrence,EventCode,EventDetailCode,Message,ApplicationPath," & _
            "ApplicationVirtualPath,MachineName,RequestUrl,ExceptionType,Details) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
            DAP.AddParameter("@EventId", raisedEvent.EventID.ToString, DbType.String)
            DAP.AddParameter("@EventTimeUtc", raisedEvent.EventTimeUtc, DbType.DateTime)
            DAP.AddParameter("@EventTime", raisedEvent.EventTime, DbType.DateTime)
            DAP.AddParameter("@EventType", raisedEvent.GetType.ToString, DbType.String)
            DAP.AddParameter("@EventSequence", raisedEvent.EventSequence, DbType.Decimal)
            DAP.AddParameter("@EventOccurrence", raisedEvent.EventOccurrence, DbType.Decimal)
            DAP.AddParameter("@EventCode", raisedEvent.EventCode, DbType.Int64)
            DAP.AddParameter("@EventDetailCode", raisedEvent.EventDetailCode, DbType.Int64)
            DAP.AddParameter("@Message", raisedEvent.Message, DbType.String)
            If Not raisedEvent.ApplicationInformation Is Nothing Then
                DAP.AddParameter("@ApplicationPath", raisedEvent.ApplicationInformation.ApplicationPath.ToString, DbType.String)
                DAP.AddParameter("@ApplicationVirtualPath", raisedEvent.ApplicationInformation.ApplicationVirtualPath.ToString, DbType.String)
                DAP.AddParameter("@MachineName", raisedEvent.ApplicationInformation.MachineName.ToString, DbType.String)
            Else
                DAP.AddParameter("@ApplicationPath", "", DbType.String)
                DAP.AddParameter("@ApplicationVirtualPath", "", DbType.String)
                DAP.AddParameter("@MachineName", "", DbType.String)
            End If
            If Not System.Web.HttpContext.Current Is Nothing Then
                DAP.AddParameter("@RequestUrl", System.Web.HttpContext.Current.Request.Url.AbsolutePath, DbType.String)
            Else
                DAP.AddParameter("@RequestUrl", "Unknown", DbType.String)
            End If
            Select Case raisedEvent.GetType.ToString
                Case Is = "System.Web.Management.WebRequestErrorEvent"
                    DAP.AddParameter("@ExceptionType", CType(raisedEvent, System.Web.Management.WebRequestErrorEvent).ErrorException.ToString, DbType.String)
                Case Is = "System.Web.Management.WebErrorEvent"
                    DAP.AddParameter("@ExceptionType", CType(raisedEvent, System.Web.Management.WebErrorEvent).ErrorException.ToString, DbType.String)
                Case Else
                    DAP.AddParameter("@ExceptionType", raisedEvent.GetType.ToString, DbType.String)
            End Select
            DAP.AddParameter("@Details", String.Empty, DbType.String)
            DAP.ExecuteNonQuery(sSQL)
        Finally
            DAP.Dispose()
        End Try
    End Sub

    Public Overrides Sub Shutdown()

    End Sub
End Class
